﻿//document.execCommand("BackgroundImageCache", false, true);   

var appPath;   

function go()
{
}

var firstModule;

function changeSelect(sender)
{
    
    var li = sender.parentNode; 
    
    if(firstModule == null)
    {
        firstModule = sender;        
    }           
   
     //上一个两边的分隔符加上
    var pre = firstModule.parentNode.previousSibling;       
    if(pre != null)
    {
        pre.className = "separator";
    }
    
    var next = firstModule.parentNode.nextSibling;
    if(next != null)
    {
        next.className = "separator";
    }   
    
    //变换背景    
    firstModule.className = "";   
    sender.className = "on";     
    
     //当前选中的两边的分隔符去掉
    pre = li.previousSibling;
    if(pre != null)
    {
        pre.className = "select_separator";
    }
    
    next = li.nextSibling;
    if(next != null)
    {
        next.className = "select_separator";
    }
    
    firstModule = sender;
    sender.blur();
}

function navModule1(id, strUrl, initModuleID)
{
    var sender = document.getElementById("menu" + id);
    if (sender == firstModule && initModuleID == null)
    {
        return;
    }
    
    //导航页面

    var tdLeft = document.getElementById("tdLeft");
    if (strUrl == "" || strUrl == "#")
    {
        strUrl = "Left.aspx?UpperID=" + id;
        
        var xmlhttp = getXmlHttp();
        xmlhttp.onreadystatechange = function()
        {

            if (xmlhttp.readyState == 4)
            {
                if (xmlhttp.status == 200)
                {
                    if (xmlhttp.responseText == "timeout")//超时
                    {
                        alert("服务器超时，请重新登录");
                        window.location.href = getAppPath() + "login.aspx";                       
                        return;
                    }

                    eval(xmlhttp.responseText);
                    var moduleHtml = "<div id='divLeft' class='left_body_bg'>";


                    for (var i = 0; i < modules.length; i++)
                    {
                        var memu2 = modules[i];

                        moduleHtml += "<div class='module2' style=''><a href='javascript:void(0);' class='collapse' id='exp" + memu2.ID + "'  onclick='expend(this,\"sub" + memu2.ID + "\");'>&nbsp;</a><a id='menu" + memu2.ID + "' level='2' NavUrl='" + memu2.NavUrl + "' href='javascript:go()' hidefocus='hidefocus' onclick='navModule2(this,\"" + memu2.NavUrl + "\")'>" + memu2.Name + "</a></div>";
                        moduleHtml += "<div id = 'sub" + memu2.ID + "' style='display:none; '>";

                        for (var j = 0; j < memu2.SubModules.length; j++)
                        {
                            var menu3 = memu2.SubModules[j];
                            moduleHtml += "<div class='module3'><a href='javascript:go()' id='menu" + menu3.ID + "' level='3' NavUrl='" + menu3.NavUrl + "' hidefocus='hidefocus' onmouseover='mouseOver(this)' onmouseout='mouseOut(this)' onclick='navModule3(this,\"" + menu3.NavUrl + "\");'><b style ='background-image:url(" + menu3.IconUrl + ");'></b><span id='span" + menu3.ID + "'>" + menu3.Name + "</span></a></div>"
                        }
                        moduleHtml += "</div>";
                    }

                    moduleHtml += "</div>";

                    tdLeft.innerHTML = moduleHtml;

                    if (initModuleID != null)
                    {
                        var tmp = document.getElementById("menu" + initModuleID);
                        if (tmp.level == 2)
                        {
                            navModule2(tmp, tmp.getAttribute("NavUrl"));

                        }
                        else
                        {
                            navModule3(tmp, tmp.getAttribute("NavUrl"));

                        }
                    }
                    else if (modules.length > 0)
                    {
                        if (modules[0].SubModules.length > 0)
                        {
                            navModule3(document.getElementById("menu" + modules[0].SubModules[0].ID), modules[0].SubModules[0].NavUrl);
                        }
                        else
                        {
                            navModule2(document.getElementById("menu" + modules[0].ID), modules[0].NavUrl);
                        }
                    }
                    changeSelect(sender);//修改主菜单的选中状态

                }
                else
                {
                    alert(xmlhttp.status);
                }
            }
        }

        xmlhttp.open("get", "Left.aspx?UpperID=" + id); //true 是异步
        xmlhttp.send(null);
    }
    else
    {
       
        tdLeft.innerHTML = '<iframe id="divLeft" name="frameLeft" frameborder="0" height="100%"> </iframe>';
        document.getElementById("divLeft").contentWindow.location.href = strUrl;
        changeSelect(sender); //修改主菜单的选中状态
        
    }
   
}     


var thirdModule;

var thirdModulesDiv;
function expend(sender, strThirdModulesDiv)
{
    if(typeof(sender) == "string")
    {
        sender = document.getElementById(sender);
    }
    
    var who = document.getElementById(strThirdModulesDiv);
   
    if(who != null)
    {                
        if(who.style.display == "none")
        {
            who.style.display = "";
            sender.className = "expend";
        }
        else
        {
            who.style.display = "none";
            sender.className = "collapse";
        }
        thirdModulesDiv = who ;
    }
    
    sender.blur();
}
function navModule2(sender,strUrl)
{
   
    if(strUrl == "" || strUrl == "#")
    {
        if (document.getElementById(sender.id.replace("menu", "sub")).childNodes.length == 0)//判断子模块（第三级模块）是否为0
        {
            alert("2[" + sender.innerHTML + "]本版本暂不提供");
        }
        else
        {
            expend(sender.previousSibling, sender.id.replace("menu", "sub"));
        }
    }
    else
    {
        setMainUrl(strUrl);
        if (thirdModule != null)
        {
            thirdModule.className = "";
            thirdModule = null;
        }
    }
    sender.blur();
    
}

function navModule3(sender, strUrl)
{
    if(thirdModule != null)
    {
        thirdModule.className = "";
    }
    sender.parentNode.parentNode.style.display = "";
    
    var parentNode = sender.parentNode.parentNode.previousSibling.getElementsByTagName("a")[0];

    
    if(parentNode != null)
    {
        parentNode.className = "expend";
    }
    sender.className="on";

   
    if (strUrl != null & strUrl != "")
    {
        setMainUrl(strUrl);
    }
    else
    {
        alert("3[" + sender.innerText + "]本版本暂不提供");
    }

    thirdModule = sender;
    sender.blur();
}

function setMainUrl(url)
{
    document.getElementById("tdMain").innerHTML = '<iframe id="frameMain" frameborder="0" name="frameMain" src=""></iframe>';
    window.setTimeout(function()
    {
        document.getElementById("frameMain").contentWindow.location.href = url;
    }, 10);//在firefox下面直接调用会出错
    
}


function mouseOver(sender)
{
    sender.className = "on";
}


function mouseOut(sender)
{
    if(sender != thirdModule)
    {
        sender.className = "";
    }
}



//选择某一个模块


//在二级模块为id的下面增加三级子模块
// 如果isUpdate为true，则就替换老的
function addModule3(id,html,isUpdate)
{
   
    var win = window.frames["frameLeft"];
    
    if (win == null)
    {
        win = top;
    }
    var parentDiv = win.document.getElementById("sub" + id);
    var extDiv = win.document.getElementById("sub" + id + "ext");  
    if(html == null || html == "")
    {
        if(extDiv != null)
        {
            extDiv.outerHTML = "";
            extDiv = null;
        }
    }
    else
    {
        if(extDiv == null)
        {
            extDiv = document.createElement("div");
            extDiv.id = "sub" + id + "ext";
            parentDiv.appendChild(extDiv);      
            extDiv.innerHTML = html;          
        }
        else
        {
            if(isUpdate)
            {
                extDiv.innerHTML = html;
            }
        }
            
    }
}

//页面加载后初始化左边的选择状态
function initLeft(moduleID)
{
    var expend = document.getElementById("menu" + moduleID);
    expend.click();
    var thirdLnk = document.getElementById("sub" + moduleID).getElementsByTagName("a")[0];
    if (thirdLnk == null)
    {
        expend.nextSibling.click();
    }
    else
    {
        thirdLnk.click();
    }

}


function selectModule(id)
{
    var menu = document.getElementById("menu" + id);

    var url = menu.getAttribute("NavUrl");
    var level = menu.getAttribute("level");

   // alert(url + "," + level);

    if (level == "3")
    {
        navModule3(menu, url);
    }
    else if(level == "2")
    {
        navModule2(menu, url);
    }
    
}